import pymysql

# 打开数据库
db = pymysql.connect(
    "localhost", "root", "root", "jianshu"
)
# 创建游标对象
cursor = db.cursor()
# 使用execute()方法执行sql语句
sql = """
    insert into article(id,title,content,author,pub_time,article_id,origin_url,avatar) 
    values(null,'test','你好','pr','2019-10-6','12','hello','1231231')
"""
lily = ('lily', 'topsion', 20, '女', 1230)
sql2 = """
    insert into employee(first_name, last_name, age, sex, income)
    values('{}','{}','{}','{}','{}')
""".format(*lily)
print(sql2)
try:
    # 执行sql语句
    cursor.execute(sql2)
    # 提交到数据库执行
    db.commit()
except OSError as err:
    # 如果发生错误则回滚
    print(err)
    db.rollback()
# 关闭数据库连接
db.close()
